package models

import (
	"gorm.io/gorm"
	"order_srv/basic/config"
	"time"
)

// 取消表
type OrderCancellation struct {
	Id              uint64         `gorm:"column:id;type:bigint UNSIGNED;primaryKey;not null;" json:"id"`
	OrderId         uint64         `gorm:"column:order_id;type:bigint UNSIGNED;comment:订单id;not null;" json:"order_id"`                            // 订单id
	Reason          string         `gorm:"column:reason;type:varchar(255);comment:取消原因;not null;" json:"reason"`                                   // 取消原因
	Liability       uint8          `gorm:"column:liability;type:tinyint UNSIGNED;comment:乘客是否有责 1-否 2-是;not null;default:1;" json:"liability"`     // 乘客是否有责 1-否 2-是
	CancellationFee float64        `gorm:"column:cancellation_fee;type:decimal(10, 2);comment:取消费(无取消则空显示);default:0.00;" json:"cancellation_fee"` // 取消费(无取消则空显示)
	UserId          uint64         `gorm:"column:user_id;type:bigint UNSIGNED;comment:操作人id;not null;" json:"user_id"`                             // 操作人id
	Identity        uint8          `gorm:"column:identity;type:tinyint UNSIGNED;comment:身份 1-乘客 2-司机 3-管理员;not null;" json:"identity"`             // 身份 1-乘客 2-司机 3-管理员
	Status          uint8          `gorm:"column:status;type:tinyint UNSIGNED;comment:状态 1-待审核 2-已批准 3-已拒绝;not null;default:1;" json:"status"`     // 状态 1-待审核 2-已批准 3-已拒绝
	CreatedAt       time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt       time.Time      `gorm:"column:updated_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt       gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (c *OrderCancellation) TableName() string {
	return "order_cancellation"
}
func (c *OrderCancellation) CreateCancel() error {
	return config.DB.Create(&c).Error
}

// 取消删除
func (c *OrderCancellation) DeleteCancel() error {
	return config.DB.Where("id = ?", c.Id).Delete(&c).Error
}

// 取消详情
func (c *OrderCancellation) GetCancelDetail(Id int) error {
	return config.DB.Where("id = ?", Id).Limit(1).Find(&c).Error
}

// 取消列表
func (c *OrderCancellation) GetCancelList(page, pageSize int) (GetCancelList []*OrderCancellation, err error) {
	err = config.DB.Where("status = ?", c.Status).Scopes(Paginate(page, pageSize)).Find(&GetCancelList).Error
	return
}

// 取消订单修改状态
func (c *OrderCancellation) UpdateOrderCancellationStatus(Id int) error {
	return config.DB.Model(&OrderCancellation{}).Where("id = ?", Id).Update("status", c.Status).Error
}
